(function($){
$.fn.lightdrag = function(delay) {
if (typeof(delay) == 'undefined') delay = 5;
var a = false, // drag active
d = $(document), // document jQuery
e = $(this), // current element
c = e.css('cursor'), // current cursor
p = [null,null], // clicked point
m = null, // current mouseDown event
t = null, // timeout id
bl = function(e) { // blocks an event (to prevent selecting text)
try { // will fail with IE & timeout
e.stopPropagation();
e.preventDefault();
} catch (e) {}; // but will still remain usable
},
md = function(e_md) { // mousedown
bl(e_md); // text selection would occur if not blocked here
if (a) { // drag active
e.css({'opacity':'0.7','cursor':'move'});
//e.css('cursor', 'move');
d.bind('mousemove', mm);
p = [e_md.clientX - e.position().left, e_md.clientY - e.position().top];
e.trigger('dragstart');
} else { // just wait
m = e_md;
t = setTimeout(to, delay);
}
},
mu = function(e_mu) { // mouseup
if (a) { // drag active
bl(e_mu);
d.unbind('mousemove', mm);
e.css('opacity', '1')
.css('cursor', c)
.css('outline', 'none')
.trigger('dragend');
}
a = false;
clearTimeout(t);
},
mm = function(e_mm) { // mousemove
bl(e_mm);
e.css('left', (e_mm.clientX - p[0]) + 'px')
.css('top', (e_mm.clientY - p[1]) + 'px');
},
to = function() { // timeout
a = true;
if (m) md(m);
};
e.bind('mousedown', md).bind('mouseup', mu);
return e;
};
})(jQuery);